package com.zhuiyun.project.api.sysuser.controller;

import com.wf.captcha.base.Captcha;
import com.zhuiyun.project.annotation.OptLog;
import com.zhuiyun.project.api.sysuser.entiy.SysUser;
import com.zhuiyun.project.api.sysuser.model.SysUserModel;
import com.zhuiyun.project.api.sysuser.service.SysUserService;
import com.zhuiyun.project.common.controller.BaseController;
import com.zhuiyun.project.common.errorCode.CommonException;
import com.zhuiyun.project.common.errorCode.EmErrorCode;
import com.zhuiyun.project.util.ObjectUtils;
import com.zhuiyun.project.util.UserUtils;
import com.zhuiyun.project.util.constants.CommonConstants;
import com.zhuiyun.project.common.response.CommonResult;
import com.zhuiyun.project.util.CodeUtils;
import com.zhuiyun.project.util.RedisUtil;
import com.zhuiyun.project.util.pageUtil.PageRequest;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.MultipartRequest;

import javax.servlet.http.HttpServletRequest;
import java.util.*;

/**
 * @ClassName SysUserController
 * @Description 用户管理 用于系统的登录  /login 默认登录地址 logout 登出地址
 * @Author zcy
 * @Date 2023/3/28 14:17
 **/
@RestController
@Api(tags = "用户管理")
@RequestMapping("/sysUser")
@Slf4j
@CrossOrigin
public class SysUserController extends BaseController {

    @Autowired
    SysUserService sysUserService;
    /**
     * 保存用户
     * @param sysUserModel
     * @return
     */
    @PostMapping("/saveUser")
    @ApiOperation(value = "用户管理的新增方法")
    @OptLog(modular = "用户管理", type = CommonConstants.ADD, describe = "新增用户")
    public CommonResult saveUser(SysUserModel sysUserModel, MultipartFile avatar) throws CommonException {
        log.info("新增用户");
        return sysUserService.saveUser(sysUserModel,avatar);
    }
    /**
     * 修改用户信息
     * @param sysUserModel
     * @return
     */
    @PutMapping("/updateUser")
    @ApiOperation(value = "用户管理的修改方法")
    @OptLog(modular = "用户管理", type = CommonConstants.UPDATE, describe = "修改用户")
    public CommonResult updateUser(@RequestBody SysUserModel sysUserModel) throws CommonException {
        log.info("修改用户");
        return sysUserService.updateUser(sysUserModel);
    }
    /**
     * 修改用户信息 管理员改
     * @param sysUserModel
     * @return
     */
    @PutMapping("/updateUserByPc")
    @ApiOperation(value = "用户管理的修改方法 管理员改")
    @OptLog(modular = "用户管理", type = CommonConstants.UPDATE, describe = "修改用户")
    public CommonResult updateUserByPc(SysUserModel sysUserModel,MultipartFile avatar) throws CommonException {
        log.info("修改用户");
        return sysUserService.updateUserByPc(sysUserModel,avatar);
    }
    /**
     * 删除用户信息
     * @param id
     * @return
     */
    @DeleteMapping("/delUserById")
    @ApiOperation(value = "用户管理的删除方法")
    @OptLog(modular = "用户管理", type = CommonConstants.DELETE, describe = "删除用户")
    public CommonResult delUserById(Integer id) throws CommonException {
        log.info("删除用户信息");
        return sysUserService.delUserById(id);
    }
    /**
     * 查询用户信息
     * @param sysUserModel
     * @return
     */
    @GetMapping("/getUserByPage")
    @ApiOperation(value = "用户管理的分页查询方法")
    @OptLog(modular = "用户管理", type = CommonConstants.SELECT, describe = "分页查询")
    public CommonResult getUserByPage(SysUserModel sysUserModel, PageRequest pageRequest) throws CommonException {
        log.info("分页查询");
        return sysUserService.getUserByPage(sysUserModel,pageRequest);
    }

    /**
     * @Author songyongcheng
     * 获取验证麻(返回的img属性可以直接放到图片得src中)登陆的时候需要携带code到(前端取data中的imgCode)
     * @Date 2023/3/30 14:10
     * @Return
     */
    @GetMapping("/code")
    @ApiOperation(value = "获取验证码")
    public CommonResult getCode(){
        // 调用验证码生成工具类
        Captcha captcha = CodeUtils.switchCaptcha();
        String captchaValue = captcha.text();
        String key = CommonConstants.CACHE_USER_PASSWORD_CODE+captchaValue;
        // 保存Redis中过期时间120秒
        RedisUtil.set(key,1,120);
        // 验证码信息
        return CommonResult.ok(captcha.toBase64());
    }


    /**
     * @description:根据用户查询权限
     * @author: dongdong
     * @date: 2023/4/3 20:01
     * @param:
     * @return: com.zhuiYun.project.common.response.CommonResult
     **/
    @GetMapping("/getPowerByUser")
    @ApiOperation(value = "根据用户查询权限")
    @OptLog(modular = "用户", type = CommonConstants.SELECT, describe = "根据用户查询权限")
    public CommonResult getPowerByUser()  {
        return sysUserService.getPowerByUser();
    }



    /**
     * @description:获取员工登录信息
     * @author: dongdong
     * @date: 2023/4/6 10:05
     * @param: []
     * @return: com.zhuiYun.project.common.response.CommonResult
     **/
    @GetMapping("/getLoginUser")
    @ApiOperation(value = "获取员工登录信息")
    @OptLog(modular = "后台管理", type = CommonConstants.SELECT, describe = "获取员工登录信息")
    public CommonResult getLoginUser () throws CommonException{
        return CommonResult.ok(UserUtils.getUser());
    }


    /**
     * 查询注销用户信息
     * @param sysUserModel
     * @return
     */
    @GetMapping("/getUserRetrieveByPage")
    @ApiOperation(value = "用户管理的查询注销用户信息")
    @OptLog(modular = "用户管理", type = CommonConstants.SELECT, describe = "查询注销用户信息")
    public CommonResult getUserRetrieveByPage(SysUserModel sysUserModel, PageRequest pageRequest) throws CommonException {
        log.info("分页查询");
        return sysUserService.getUserRetrieveByPage(sysUserModel,pageRequest);
    }
    /**
     * @description:给用户授权
     * @author: dongdong
     * @date: 2023/4/6 20:02
     * @param: [userId, menuIds]
     * @return: com.zhuiYun.project.common.response.CommonResult
     **/
    @PostMapping("/userPower")
    @ApiOperation(value = "给用户授权")
    @OptLog(modular = "用户管理", type = CommonConstants.UPDATE, describe = "给用户授权")
    public CommonResult userPower(Integer userId , Integer[] menuIds){
        List<Integer> integers = new ArrayList<>();
        if(menuIds!=null){
            integers = Arrays.asList(menuIds);
        }
        return sysUserService.userPower(userId,integers);
    }
    /**
     * @description:查询用户权限
     * @author: dongdong
     * @date: 2023/4/6 20:58
     * @param: [userId]
     * @return: com.zhuiYun.project.common.response.CommonResult
     **/
    @GetMapping("getUserPower")
    @ApiOperation(value = "查询用户权限")
    @OptLog(modular = "用户管理", type = CommonConstants.SELECT, describe = "查询用户权限")
    public CommonResult getUserPower(Integer userId){
        return sysUserService.getUserPower(userId);
    }

    /**
     * @param imageFile
     * @param sysUserModel
     * @Author kdj
     * @Description 修改用户头像
     * @Date 2023/6/13 17:20
     * @Return com.zhuiyun.project.common.response.CommonResult
     */
    @PostMapping("/updateUserFile")
    @ApiOperation(value = "用户管理的修改头像方法")
    @OptLog(modular = "用户管理", type = CommonConstants.UPDATE, describe = "修改用户头像")
    public CommonResult updateUserFile(HttpServletRequest request, MultipartRequest imageFile, SysUserModel sysUserModel) throws CommonException {
        //接受头像文件存储
        MultipartFile file = imageFile.getFile("file");
        //调用修改方法
        return sysUserService.updateUserFile(request, file, sysUserModel);
    }

    /**
     * @param sysUserModel
     * @Author kdj
     * @Description 修改用户密码
     * @Date 2023/6/13 17:20
     * @Return com.zhuiyun.project.common.response.CommonResult
     */
    @PutMapping("/updatePassword")
    @ApiOperation(value = "用户管理的修改密码方法")
    @OptLog(modular = "用户管理", type = CommonConstants.UPDATE, describe = "修改用户密码")
    public CommonResult updatePassword(@RequestBody SysUserModel sysUserModel) throws CommonException {
        //调用修改方法
        return sysUserService.updatePassword(sysUserModel);
    }


    /**
     * 根据id查询用户信息
     *
     * @param id
     * @return
     */
    @GetMapping("/getSysUserById")
    @ApiOperation(value = "根据id查询用户信息")
    @OptLog(modular = "用户管理", type = CommonConstants.SELECT, describe = "根据id查询用户信息")
    public CommonResult getSysUserById(@RequestParam(value = "id") Integer id) {
        log.info("根据id查询用户信息");
        SysUser user = sysUserService.getSysUserById(id);
        if (ObjectUtils.isNotEmpty(user)) {
            return CommonResult.ok(user);
        }
        return CommonResult.error(EmErrorCode.ID_NOT_OBJECT);
    }

    /**
     * @Author kdj
     * @Description 判断密码是否一致
     * @Date 2023/6/14 10:56
     * @Return com.zhuiyun.project.common.response.CommonResult
     * @param id
     * @param password
     */
    @GetMapping("/checkOldPassword")
    @ApiOperation(value = "用户管理的判断密码是否一致")
    @OptLog(modular = "用户管理", type = CommonConstants.UPDATE, describe = "判断密码是否一致")
    public CommonResult checkOldPassword(Integer id, String password) throws CommonException {
        //判断密码是否一致
        return sysUserService.checkOldPassword(id, password);
    }


}
